package com.xinzhidi.web.equipment.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.web.equipment.domain.EquipmentArchivesRestore;
import com.xinzhidi.web.equipment.dto.EquipmentArchivesRestoreAddDto;
import com.xinzhidi.web.equipment.dto.EquipmentArchivesRestoreListDto;
import com.xinzhidi.web.equipment.dto.EquipmentArchivesRestoreUpdDto;
import com.xinzhidi.web.equipment.mapper.EquipmentArchivesRestoreMapper;
import com.xinzhidi.web.equipment.service.IEquipmentArchivesRestoreService;
import com.xinzhidi.web.equipment.service.IEquipmentArchivesService;
import com.xinzhidi.web.equipment.vo.EquipmentArchivesRestoreInfoVo;
import com.xinzhidi.web.equipment.vo.EquipmentArchivesRestoreListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



/**
 * 存档恢复记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-21
 */
@Service
public class EquipmentArchivesRestoreServiceImpl extends ServiceImpl<EquipmentArchivesRestoreMapper, EquipmentArchivesRestore>  implements IEquipmentArchivesRestoreService{
    @Autowired
    private EquipmentArchivesRestoreMapper equipmentArchivesRestoreMapper;
    @Autowired
    private IEquipmentArchivesService equipmentArchivesService;

    /**
     * 查询存档恢复记录
     *
     * @param id 存档恢复记录主键
     * @return 存档恢复记录
     */
    @Override
    public EquipmentArchivesRestoreInfoVo selectEquipmentArchivesRestoreById(Long id){
        EquipmentArchivesRestore equipmentArchivesRestore = equipmentArchivesRestoreMapper.selectById(id);
        EquipmentArchivesRestoreInfoVo infoVo = new EquipmentArchivesRestoreInfoVo();
        BeanUtils.copyProperties(equipmentArchivesRestore,infoVo);
        return infoVo;
    }

    /**
     * 查询存档恢复记录列表
     *
     * @param dto 查询存档恢复记录dto
     * @return 存档恢复记录
     */
    @Override
    public TableDataInfo<EquipmentArchivesRestoreListVo> selectEquipmentArchivesRestoreList(EquipmentArchivesRestoreListDto dto){
        return TableDataInfo.getDataTable(equipmentArchivesRestoreMapper.selectListNative(dto).stream().map( domain ->{
            String info = domain.getPlotName();
            if (domain.getBuildName() != null){
                info += "-" + domain.getBuildName();
            }
            if (domain.getUnitName() != null){
                info += "-" + domain.getUnitName();
            }
            domain.setInfo(info);
            return domain;
        }).collect(Collectors.toList()));
    }

    /**
     * 新增存档恢复记录
     *
     * @param dto 新增存档恢复记录dto
     * @return 结果
     */
    @Override
    public int insertEquipmentArchivesRestore(EquipmentArchivesRestoreAddDto dto){
        LoginUser loginUser = SecurityUtils.getLoginUser();

        EquipmentArchivesRestore equipmentArchivesRestore = new EquipmentArchivesRestore();
        BeanUtils.copyProperties(dto,equipmentArchivesRestore);
        equipmentArchivesRestore.setCreateBy(loginUser.getUserId().toString());
        equipmentArchivesRestore.setCreateTime(DateUtils.getNowDate());
        restore(equipmentArchivesRestore);
        return equipmentArchivesRestoreMapper.insert(equipmentArchivesRestore);
    }

    /**
     * 修改存档恢复记录
     *
     * @param dto 修改存档恢复记录dto
     * @return 结果
     */
    @Override
    public int updateEquipmentArchivesRestore(EquipmentArchivesRestoreUpdDto dto){
        LoginUser loginUser = SecurityUtils.getLoginUser();

        EquipmentArchivesRestore equipmentArchivesRestore = new EquipmentArchivesRestore();
        BeanUtils.copyProperties(dto,equipmentArchivesRestore);
    equipmentArchivesRestore.setUpdateBy(loginUser.getUserId().toString());
        equipmentArchivesRestore.setUpdateTime(DateUtils.getNowDate());
        return equipmentArchivesRestoreMapper.updateById(equipmentArchivesRestore);
    }

    /**
     * 批量删除存档恢复记录
     *
     * @param ids 需要删除的存档恢复记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentArchivesRestoreByIds(Long[] ids){
        return equipmentArchivesRestoreMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除存档恢复记录信息
     *
     * @param id 存档恢复记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentArchivesRestoreById(Long id){
        return equipmentArchivesRestoreMapper.deleteById(id);
    }

    @Override
    public int addRestore(Long[] ids) {
        boolean b= false;
        List<EquipmentArchivesRestore> equipmentArchivesRestorea = equipmentArchivesService.listByIds(Arrays.asList(ids)).stream().map(entiry -> {
            EquipmentArchivesRestore equipmentArchivesRestore = new EquipmentArchivesRestore();
            BeanUtils.copyProperties(entiry, equipmentArchivesRestore);
            equipmentArchivesRestore.setArchivesId(entiry.getId());
            equipmentArchivesRestore.setArchivesTime(entiry.getCreateTime());
            equipmentArchivesRestore.setId(null);
            restore(equipmentArchivesRestore);
            return equipmentArchivesRestore;
        }).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(equipmentArchivesRestorea)){
            b = this.saveBatch(equipmentArchivesRestorea);
        }
        return b?1:0;
    }

    //TODO 恢复主逻辑
    /**
     * 恢复主逻辑
     */
    public void restore(EquipmentArchivesRestore equipmentArchivesRestore) {
        equipmentArchivesRestore.setStatus("成功");
        equipmentArchivesRestore.setCreateTime(new Date());
//        equipmentArchivesRestore.setStatus("失败");
    }

}
